package com.zhiyou100.controller;

import java.io.File;
import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.zhiyou100.IService.IserviceGo;
import com.zhiyou100.entity.prosGo.TbProjects;
import com.zhiyou100.entity.prosGo.TbRepay;
import com.zhiyou100.entity.prosGo.TbUser;

@Controller
public class ProsGoProController {

	@Resource
	private IserviceGo service;
	@Autowired
	private HttpServletRequest request;
	
	@ResponseBody
	@RequestMapping("progo1.do")
	public String progo1(TbUser user,TbProjects pro) {
		int id = service.progo1(user,pro);
		if (id!=-1) {
			request.getSession().setAttribute("proId", id);
			System.out.println(id);
			return "1";
		}
		return "-1";
	}
	@ResponseBody
	@RequestMapping("fileUpLoad.do")
	public String fileUpLoad(@RequestParam("file1")MultipartFile file) {
		String path = request.getSession().getServletContext().getRealPath("/images");
		System.out.println(path);
		String filename = file.getOriginalFilename();
		
		
		File upload_file = new File(path,filename);
		String filepath=null;
		try {
			filepath = "images\\"+new String(filename.getBytes(), "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		request.getSession().setAttribute("filepath", filepath);
		if (!upload_file.getParentFile().exists()) {
			upload_file.getParentFile().mkdirs();
		}
		try {
			file.transferTo(upload_file);
		} catch (Exception e) {
			return "1";        /////失败
		} 
		return "0";            //成功
	}
	@ResponseBody
	@RequestMapping("progo2.do")
	public String progo2(TbProjects pro) {
		pro.setPsFilepath((String)request.getSession().getAttribute("filepath"));
		pro.setPsId((Integer)request.getSession().getAttribute("proId"));
		boolean b = service.progo2(pro);
		if (!b) {
			return "1";   
		}
		return "0";
	}
	@ResponseBody
	@RequestMapping("progo3.do")
	public String progo3(TbProjects pro) {
		pro.setPsId((Integer)request.getSession().getAttribute("proId"));
		pro.setPsUsId((Integer)request.getSession().getAttribute("usId"));
		boolean b = service.progo2(pro);
		if (b) {
			return "0";
		}
		return "1";
	}
	@ResponseBody
	@RequestMapping("progo4.do")
	public String progo4(TbRepay repay) {
		repay.setRyPsId((Integer)request.getSession().getAttribute("proId"));
		repay.setRyUsId((Integer)request.getSession().getAttribute("usId"));
		repay.setRyStatus(1);
		boolean b = service.progo4(repay);
		return b?"1":"0";
	}
}
